﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using stdole;

namespace PlanningGIS.Draw
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DropDownFontSize : UserControl, ICommand
    {
        /// <summary>
        /// 
        /// </summary>
        IApplication m_Application = null;
        /// <summary>
        /// 
        /// </summary>
        public DropDownFontSize()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 
        /// </summary>
        #region ICommand Members

        int ICommand.Bitmap
        {
            get { return 0; }
        }

        string ICommand.Caption
        {
            get { return "DropDownFontSize"; }
        }

        string ICommand.Category
        {
            get { return "字体大小设置"; }
        }

        bool ICommand.Checked
        {
            get { return false; }
        }

        bool ICommand.Enabled
        {
            get
            {

                return (m_Application as IFramework).IsSceneCtlActive == false;
            }
        }

        int ICommand.HelpContextID
        {
            get { return 0; }
        }

        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }

        string ICommand.Message
        {
            get { return "DropDownFontSize"; }
        }

        string ICommand.Name
        {
            get { return "DropDownFontSize"; }
        }

        void ICommand.OnClick()
        {
            if (m_Application != null)
            {
                this.cbxFontSize.Text = (m_Application.Document as IMxDocument).DefaultTextFont.Size.ToString();
            }
        }

        void ICommand.OnCreate(object hook)
        {
            m_Application = hook as IApplication;
         
        }

        string ICommand.Tooltip
        {
            get { return "字体大小设置"; }
        }

        #endregion
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_Application != null)
            {
                (m_Application.Document as IMxDocument).DefaultTextFont.Size = decimal.Parse(this.cbxFontSize.Text.ToString());
                
                IActiveView m_ActiveView = (m_Application.Document as IMxDocument).ActiveView;
                IGraphicsContainerSelect pGraphicsContainerSelect = m_ActiveView as IGraphicsContainerSelect;
                if (pGraphicsContainerSelect != null)
                {
                    IEnumElement pEnumElement = pGraphicsContainerSelect.SelectedElements;
                    IElement pElement = pEnumElement.Next();
                    while (pElement != null)
                    {
                        if (pElement is ITextElement)
                        {
                            ITextElement pTextElement = (ITextElement)pElement;
                            ITextSymbol pTextSymbol = pTextElement.Symbol;
                            IFontDisp pFontDisp=pTextSymbol.Font;
                            pFontDisp.Size = decimal.Parse(this.cbxFontSize.Text);
                            pTextSymbol.Font = pFontDisp;
                            pTextElement.Symbol = pTextSymbol;
                        }
                        pElement = pEnumElement.Next();
                    }
                }
                m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
        }

        /// <summary>
        /// 输入字符只能是数字、小数点或退格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxFontSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            DevComponents.DotNetBar.Controls.ComboBoxEx txt = (DevComponents.DotNetBar.Controls.ComboBoxEx)sender;
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
                e.Handled = true;
            if (txt.Text.Contains(".") && e.KeyChar == 46)
                e.Handled = true;
            if (e.KeyChar == 13)
            {
                double fontsize = 10.0;
                if (double.TryParse(this.cbxFontSize.Text, out fontsize) && (fontsize > 0.1))
                {
                    if (m_Application != null)
                    {
                        (m_Application.Document as IMxDocument).DefaultTextFont.Size = (decimal)fontsize;

                        IActiveView m_ActiveView = (m_Application.Document as IMxDocument).ActiveView;
                        IGraphicsContainerSelect pGraphicsContainerSelect = m_ActiveView as IGraphicsContainerSelect;
                        if (pGraphicsContainerSelect != null)
                        {
                            IEnumElement pEnumElement = pGraphicsContainerSelect.SelectedElements;
                            IElement pElement = pEnumElement.Next();
                            while (pElement != null)
                            {
                                if (pElement is ITextElement)
                                {
                                    ITextElement pTextElement = (ITextElement)pElement;
                                    ITextSymbol pTextSymbol = pTextElement.Symbol;
                                    IFontDisp pFontDisp = pTextSymbol.Font;
                                    pFontDisp.Size = (decimal)fontsize;
                                    pTextSymbol.Font = pFontDisp;
                                    pTextElement.Symbol = pTextSymbol;
                                }
                                pElement = pEnumElement.Next();
                            }
                        }
                        m_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                    }
                }

                e.Handled = true;
            }

            if ((ModifierKeys & Keys.Control) == Keys.Control)
                e.Handled = false;
        }
    }
}
